/*
  json2Excel : json转excel文件
  @params JSONData : 表格json数据
  @params FileName : 需要保存的表格名称
  @params title : 自定义表格单元标题
  @params filter : 过滤行 不需要导出的数据列
  @params bigTitle : 表格大标题
*/
function json2Excel (JSONData, FileName, title, filter, bigTitle) {
  if (!JSONData) return;

  let arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
  let excel = "<table>";
  let len = 0;
  let dataLen = 0;
  const firstTitleStyle = 'text-align: center; background-color: #2d3436; font-weight: bold; color: #ffffff; font-size: 20px; height: 60px;';
  const secondTitleStyle = 'background-color: #b2bec3; color: #000000; text-align: center;';
  const itemTitleStyle = 'background-color: #636e72;';
  const itemStyle = 'text-align: center; background-color: #dfe6e9; white-space: pre-line;';

  for (let key in arrData[0]) {
    if (typeof arrData[0][key] !== 'boolean') {
      dataLen ++
    }
  };
  if (filter && filter.length) {
    len = dataLen - filter.length
  } else {
    len = dataLen
  };

  //#region 设置表格大标题
  if (bigTitle) {
    let row = '<tr>';
    row += '<td style="' + firstTitleStyle + '" colspan="' + len + '">' + bigTitle + '</td>'
    row += '</tr>';
    excel += row
  };
  //#endregion
  //#region 设置表头
  let row = "<tr>";
  // 是否使用自定义标题
  if (title && title.length) {
    for (let i in title) {
      row += "<th align='center' style='" + itemTitleStyle + "'>" + title[i] + '</th>';
    }
  } else {
    for (let i in arrData[0]) {
      row += "<th align='center' style='" + itemTitleStyle + "'>" + i + '</th>';
    }
  };
  excel += row + "</tr>";
  //#endregion

  //#region 渲染表格数据
  for (let i = 0; i < arrData.length; i++) {
    let row = "<tr>";

    if (arrData[i].isTitle) {
      let value = arrData[i].label == null ? "" : arrData[i].label;
      row += '<td style="' + secondTitleStyle + '" colspan="' + len + '">' + value + '</td>';
    } else {
      for (let index in arrData[i]) {
        //判断是否有过滤行
        if (filter) {
          if (filter.indexOf(index) == -1) {
            let value = arrData[i][index] == null ? "" : arrData[i][index];
            row += '<td style="' + itemStyle + '">' + value + '</td>';
          }
        } else {
          let value = arrData[i][index] == null ? "" : arrData[i][index];
          row += '<td style="' + itemStyle + '">' + value + '</td>';
        }
      };
    }
    excel += row + "</tr>";
  };
  excel += "</table>";
  //#endregion

  //#region 生成伪excel文件(html文件)
  let excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
  excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
  excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
  excelFile += '; charset=UTF-8">';
  excelFile += "<head>";
  excelFile += "<!--[if gte mso 9]>";
  excelFile += "<xml>";
  excelFile += "<x:ExcelWorkbook>";
  excelFile += "<x:ExcelWorksheets>";
  excelFile += "<x:ExcelWorksheet>";
  excelFile += "<x:Name>";
  excelFile += "{worksheet}";
  excelFile += "</x:Name>";
  excelFile += "<x:WorksheetOptions>";
  excelFile += "<x:DisplayGridlines/>";
  excelFile += "</x:WorksheetOptions>";
  excelFile += "</x:ExcelWorksheet>";
  excelFile += "</x:ExcelWorksheets>";
  excelFile += "</x:ExcelWorkbook>";
  excelFile += "</xml>";
  excelFile += "<![endif]-->";
  excelFile += "</head>";
  excelFile += "<body>";
  excelFile += excel;
  excelFile += "</body>";
  excelFile += "</html>";
  //#endregion

  //#region 利用a标签下载
  let url = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);

  let link = document.createElement("a");
  link.href = url;

  link.style = "visibility:hidden";
  link.download = FileName + ".xls";

  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  //#endregion
};
export default json2Excel
